package com.xpsoft.oa.service.produce.impl;


import org.springframework.beans.factory.annotation.Autowired;

import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.oa.dao.produce.MaterialInStockDao;
import com.xpsoft.oa.dao.produce.ProjectRepertoryDao;
import com.xpsoft.oa.model.produce.MaterialInStock;
import com.xpsoft.oa.model.produce.ProjectRepertory;
import com.xpsoft.oa.service.produce.MaterialInStockService;

public class MaterialInStockServiceImpl extends BaseServiceImpl<MaterialInStock> implements MaterialInStockService{
	private MaterialInStockDao dao;
	@Autowired
	private ProjectRepertoryDao projectRepertoryDao;
	public MaterialInStockServiceImpl(MaterialInStockDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveMaterialInStock(MaterialInStock materialInStock) {
		// TODO Auto-generated method stub
		dao.save(materialInStock);
		//判断物资是否存在项目仓库 ，存在就加上数量，不存在就加数据
		ProjectRepertory projectRepertory = projectRepertoryDao.getRepertory(materialInStock.getProject().getId(), materialInStock.getMaterial().getId());
		if(projectRepertory!=null){
			projectRepertory.setNum(projectRepertory.getNum()+materialInStock.getNum());
		}else{
			projectRepertory = new ProjectRepertory();
			projectRepertory.setMaterial(materialInStock.getMaterial());
			projectRepertory.setProject(materialInStock.getProject());
			projectRepertory.setNum(materialInStock.getNum());
		}
		projectRepertoryDao.save(projectRepertory);
	}

}